package com.fullstackdaytwo.registerlogin.util;

import com.fullstackdaytwo.registerlogin.pojo.AbstractUser;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.*;

/**
 * @Description: 文件操作类
 * @author: Aron
 * @date: 2021.10.13
 */

public class FileOperator {
    private final static Logger logger = LoggerFactory.getLogger(FileOperator.class);

    /**
     * @Description: 文件写入
     * @Param: [list, path]
     * @Author: Aron
     * @Date: 2021/10/13
     */
    public static boolean fileWriter(AbstractUser user) {
        String path = "src/main/resources/" + user.getUserName() + ".txt";
        String str = user.getUserType() + "," + user.getUserName() + "," + user.getPassWord();
        try (BufferedWriter out = new BufferedWriter(new FileWriter(path))) {
            out.write(str);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return true;
    }

    /**
     * @Description: 文件读取
     * @Param: [path]
     * @return: java.util.ArrayList<java.lang.String>
     * @Author: Aron
     * @Date: 2021/10/13
     */

    public static String fileReader(AbstractUser user) {
        String path = "src/main/resources/" + user.getUserName() + ".txt";
        File file = new File(path);
        String strs = null;
        if (file.exists()) {
            try (BufferedReader in = new BufferedReader(new FileReader(file))) {
                strs = in.readLine();
                return strs;
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return null;
    }

}
